package work.flow.demo.config.constant;

import org.flowable.engine.IdentityService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.idm.api.Group;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.io.FileInputStream;
import java.io.IOException;

//@Configuration
public class LeaveProcessConfig {

    private static final Logger logger = LoggerFactory.getLogger(LeaveProcessConfig.class);

    @Bean(name = {"LeaveProcessEngine"})
    public ProcessEngine processEngine() {
        logger.warn("=======>processEngine初始化了<=======");
        // 1. 初始化流程引擎
        ProcessEngineConfiguration config = ProcessEngineConfiguration//
                .createStandaloneProcessEngineConfiguration()//
                .setJdbcUrl("jdbc:postgresql://172.26.2.97:5432/test_01?useSSL=true&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8")//
                .setJdbcUsername("postgres")//
                .setJdbcPassword("abc123456")//
                .setJdbcDriver("org.postgresql.Driver")//
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);//表示启用自动更新
        ProcessEngine processEngine = config.buildProcessEngine();
        // 2. 部署流程定义
        RepositoryService repositoryService = processEngine.getRepositoryService();
        try {
            // 部署流程定义文件
            Deployment deployment = repositoryService.createDeployment()//
                    .addInputStream("LeaveProcess.bpmn20.xml", new FileInputStream("D:/process/LeaveProcess.bpmn20.xml"))//
                    .name("流程部署")//
                    .deploy();//
            logger.warn("流程部署成功，部署ID: {}", deployment.getId());
        } catch (IOException e) {
            logger.warn("流程部署失败: {}", e.getMessage());
        }
        return processEngine;
    }
}
